﻿using HelloWorld.RobotFights;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace RobotsFight.RobotImplementation
{
    public class McRageRobot:Robot
    {
        public List<int[]> Memory = new List<int[]>();
        public int[,]            s = new int[1, 1];


        
        private bool MemoryContainsCoordinate(int x, int y)
        {
            foreach(int[] item in this.Memory)
            {
                if (item[0] == x && item[1] == y)
                    return true;
            }
            return false;
        }

        public override void DoStep(Func<int, int, Robot,int> checkIfCellEmpty)
        {
            int[] s = { x, y };
            int check = 0;
            Memory.Add(s);
            Random rnd = new Random();
            
            
                                
                int m = rnd.Next(0, 4);
                
                 if (MemoryContainsCoordinate(x - 1, y) && MemoryContainsCoordinate(x + 1, y) && MemoryContainsCoordinate(x, y-1) && MemoryContainsCoordinate(x, y+1))
                {
                   
                    if (m == 1)
                    {
                        if (check == checkIfCellEmpty(x - 1, y, this))
                        {
                            MoveLeft();
                        }
                        else
                        {
                            m++;
                        }
                        
                    }
                    else if (m == 2)
                    {
                        if (check == checkIfCellEmpty(x + 1, y, this))
                        {
                            MoveRight();
                        }
                        else
                        {
                            m++;
                        }
                        
                    }
                    else if (m == 3)
                    {
                        if (check == checkIfCellEmpty(x, y-1, this))
                        {
                            MoveTop();
                        }
                        else
                        {
                            m++;
                        }
                        
                    }
                    else
                    {
                        if (check == checkIfCellEmpty(x, y+1, this))
                        {
                            MoveBottom();
                        }
                        else
                        {
                            m++;
                        }
                        
                    }
                }
                else
                {
                    
                    if (m == 1)
                    {
                        if (MemoryContainsCoordinate(x - 1, y))
                        {
                           
                            return;
                        }
                        else
                        {
                            if (check == checkIfCellEmpty(x - 1, y, this))
                            {
                                MoveLeft();
                            }
                            else
                            {
                                m++;
                            }
                            
                        }

                    }
                    else if (m == 2)
                    {
                        if (MemoryContainsCoordinate(x, y + 1))
                        {
                            return;
                        }
                        else
                        {
                            if (check == checkIfCellEmpty(x + 1, y, this))
                            {
                                MoveRight();
                            }
                            else
                            {
                                m++;
                            }
                            
                        }

                    }
                    else if (m == 3)
                    {
                        if (MemoryContainsCoordinate(x, y+1))
                        {
                            return;
                        }
                        else
                        {
                            if (check == checkIfCellEmpty(x, y + 1, this))
                            {
                                MoveBottom();
                            }
                            else
                            {
                                m++;
                            }
                        } 

                    }
                    else if (MemoryContainsCoordinate(x, y - 1))
                    {
                        
                        return;
                    }
                    else
                    {
                        if (check == checkIfCellEmpty(x, y - 1, this))
                        {
                            MoveTop();
                        }
                        else
                        {
                            m++;
                        }
                        
                    }
                
                        
                        
                }
                
                
            }
        }
    }


   


